package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.ShopPrinter;
import com.caocaog.market.service.ShopPrinterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.entity.groups.InsertGroup;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.Arrays;

/**
 * 打印机设置
 *
 * @author chechen
 * @date 2021-12-01
 */
@Slf4j
@RestController
@Api(value = "shopPrinter", tags = ShopPrinterController.TABLE_MEANING)
@RequestMapping("/shopPrinter")
public class ShopPrinterController extends GenericController<ShopPrinter, ShopPrinterService> {

	public static final String TABLE_MEANING = "打印机设置";

	private static final String PAGE = "打印机设置-分页列表查询";
	@ApiOperation(value = PAGE, notes = PAGE)
	@GetMapping(value = "/list")
	@RequiresPermissions("shopPrinter:view")
	public Result<?> queryShopPrinterPage(ShopPrinter shopPrinter, QueryRequest queryRequest) {
		Page<ShopPrinter> page = PageGenerator.createPageSort(queryRequest);
		IPage<ShopPrinter> shopPrinterIPage = super.service.queryShopPrinterPage(page, shopPrinter);
		return Result.OK(shopPrinterIPage);
	}

	private static final String ADD = "打印机设置-添加";
	@AutoLog(value = ADD)
	@ApiOperation(value = ADD, notes = ADD)
	@PostMapping(value = "/add")
	@RequiresPermissions("shopPrinter:add")
	public Result<?> add(@RequestBody @Validated({InsertGroup.class, Default.class}) ShopPrinter shopPrinter) {
		return Result.OK(ADD_SUCCESS, super.service.addShopPrinter(shopPrinter));
	}

	private static final String EDIT = "打印机设置-编辑";
	@AutoLog(value = EDIT)
	@ApiOperation(value = EDIT, notes = EDIT)
	@PutMapping(value = "/edit")
	@RequiresPermissions("shopPrinter:edit")
	public Result<?> edit(@RequestBody @Validated({UpdateGroup.class, Default.class}) ShopPrinter shopPrinter) {
		return Result.OK(EDIT_SUCCESS, super.service.updateShopPrinter(shopPrinter));
	}

	private static final String DELETE = "打印机设置-通过id删除";
	@AutoLog(value = DELETE)
	@ApiOperation(value = DELETE, notes = DELETE)
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("shopPrinter:delete")
	public Result<?> delete(@RequestParam(name = "id") String id) {
		return Result.OK(DELETE_SUCCESS, super.service.deleteShopPrinter(id));
	}

	private static final String DELETE_BATCH = "打印机设置-批量删除";
	@AutoLog(value = DELETE_BATCH)
	@ApiOperation(value = DELETE_BATCH, notes = DELETE_BATCH)
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("shopPrinter:delete")
	public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
		super.service.deleteBatchShopPrinter(Arrays.asList(ids.split(",")));
		return Result.OK(DELETE_SUCCESS);
	}

}
